#include "ObPlant.h"
#include "Collision.h"
#include "ObPlayer.h"
#include "ObBullet.h"

ObPlant::ObPlant(ObPipe* parent, IDirect3DTexture9* mtex, float px, float py, STATUS param){
	mParent = parent;
	mOldPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mWidth = 16.0f;
	mHeight = 24.0f;
	count = 0.0f;
	jumpcount = 0.0f;
	delaycount = 0.0f;
	mTex = mtex;
	mPos.x = px;
	mPos.y = py;
	mCurrentSprite = 0;
	mSpritePos.x = 10;
	mSpritePos.y = 20;

	switch (param)
	{
	case ObPlant::LEFT:
		mWidth = 24.0f;
		mHeight = 16.0f;
		//mCenter = D3DXVECTOR3(mWidth/2, -mHeight/2, 0.0f);
		mPos.x +=10;
		mRotation = D3DX_PI/2;
		break;
	case ObPlant::RIGHT:
		mWidth = 24.0f;
		mHeight = 16.0f;
		//mCenter = D3DXVECTOR3(mWidth, -mHeight/2 -4 , 0.0f);
		mPos.x -=10;
		mRotation = -D3DX_PI/2;
		break;
	case ObPlant::TOP:
		mRotation = 0.0f;
		//mCenter = D3DXVECTOR3(mWidth/2, mHeight/2-4, 0.0f);
		mPos.y -=10;
		break;
	case ObPlant::DOWN:
		mRotation = D3DX_PI;
		//mCenter = D3DXVECTOR3(mWidth/2, -mHeight, 0.0f);
		mPos.y += 10;
		break;
	default:
		break;
	}
	mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
	mOldPos.x = mPos.x;
	mOldPos.y = mPos.y;
	mStatus = param;
}

ObPlant::~ObPlant(){

}

void ObPlant::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(player->getIsGoD()){
				if(mParent->getInfo() >= 200){
					mParent->setInfo(mParent->getInfo() - 100);
				}
				mNeedCollision = false;
				mNeedDispose = true;
			}
			
		}
		break;
	case ObBullet::BL1:
		if(ObBullet* player  = dynamic_cast<ObBullet*>(object)){
			if(mParent->getInfo()>=200){
				mParent->setInfo(mParent->getInfo() - 100);
			}
			mNeedCollision = false;
			mNeedDispose = true;
		}
		break;
	default:
		break;
	}
}

void ObPlant::update(float dt){
	if(mNeedDispose) return;

	if(delaycount == 0.0f){
		if(jumpcount> 0.0f){
			jumpcount-=dt;
			if(jumpcount > 1.0f){
				switch (mStatus)
				{
				case ObPlant::LEFT:
					mPos.x -=25*dt;
					break;
				case ObPlant::RIGHT:
					mPos.x +=25*dt;
					break;
				case ObPlant::TOP:
					mPos.y +=25*dt;
					break;
				case ObPlant::DOWN:
					mPos.y -=25*dt;
					break;
				default:
					break;
				}
			}else if(jumpcount > 0.0f){
				switch (mStatus)
				{
				case ObPlant::LEFT:
					mPos.x +=25*dt;
					break;
				case ObPlant::RIGHT:
					mPos.x -=25*dt;
					break;
				case ObPlant::TOP:
					mPos.y -=25*dt;
					break;
				case ObPlant::DOWN:
					mPos.y +=25*dt;
					break;
				default:
					break;
				}
			}else{
				jumpcount = 0.0f;
				delaycount = 2.0f;
				mPos.x = mOldPos.x;
				mPos.y = mOldPos.y;
			}
		}else{
			delaycount = 1.0f;
		}
	}else{
		delaycount -=dt;
		if(delaycount<=0.0f){
			delaycount = 0.0f;
			jumpcount = 2.0f;
		}
	}

	count+=dt;

	if(count>=0.2f){
		mCurrentSprite++;
		if(mCurrentSprite>1){
			mCurrentSprite = 0;
		}
		mSpritePos.x = 10.0f + 20*mCurrentSprite;
		count = 0.0f;
	}

}
void ObPlant::render(ID3DXSprite* mSprite){
	if(mNeedDispose) return;
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	
	if(mStatus == LEFT|| mStatus == RIGHT){
		RECT rect = calRenderRECT(mSpritePos,mHeight,mWidth);
		mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	}else{
		RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
		mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	}
	
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}
void ObPlant::clear(){

}

